package app.qrcode.browser.chat

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import app.qrcode.browser.chat.adapter.Msg
import app.qrcode.browser.chat.adapter.MsgAdapter
import com.hyphenate.EMCallBack
import com.hyphenate.EMMessageListener
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import kotlinx.android.synthetic.main.activity_chat.*


class ChatActivity : AppCompatActivity(), EMMessageListener {


    private var msgAdapter: MsgAdapter? = null

    private val msgList by lazy { ArrayList<Msg>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_chat)
        initView()
        initRecyclerView()
    }

    private fun initView() {
        sendbutton.setOnClickListener {
            if (editTextContent.text.isNotEmpty()) {
                val toChatUsername = intent.getStringExtra("pName_data")
                val message =
                    EMMessage.createTxtSendMessage(editTextContent.text.toString(), toChatUsername)
                EMClient.getInstance().chatManager().sendMessage(message)
                message.setMessageStatusCallback(object : EMCallBack {
                    @SuppressLint("SetTextI18n")
                    override fun onSuccess() {
                        val content = editTextContent.text.toString()
                        if (content.isNotEmpty()) {
                            val msg = Msg(content, Msg.TYPE_SENT)
                            msgList.add(msg)
                            runOnUiThread {
                                msgAdapter?.notifyItemInserted(msgList.size - 1)
                                recyclerview.scrollToPosition(msgList.size - 1)
                                editTextContent.setText("")
                            }
                        }
                        Log.w("cdm", "ChatActivity.onSuccess -> 消息发送成功")
                    }

                    override fun onError(p0: Int, p1: String?) {
                        Log.w("cdm", "ChatActivity.onError -> 消息发送失败")
                    }

                    override fun onProgress(p0: Int, p1: String?) {

                    }

                })
            }
        }
        chatname.text = intent.getStringExtra("pName_data")
    }

    private fun initRecyclerView() {
        recyclerview.layoutManager = LinearLayoutManager(this)
        msgAdapter = MsgAdapter(msgList)
        recyclerview.adapter = msgAdapter
    }

    @SuppressLint("SetTextI18n")
    override fun onMessageReceived(p0: MutableList<EMMessage>?) {
        p0?.forEach {
            runOnUiThread {
                val content = (it.body as EMTextMessageBody).message
                val msg= Msg(content, Msg.TYPE_RECEIVED)
                msgList.add(msg)
                msgAdapter?.notifyItemInserted(msgList.size - 1)
                recyclerview.scrollToPosition(msgList.size - 1)
                Log.w("cdm", "ChatActivity.onMessageReceived -> ${(it.body)}")
                Log.w("cdm", "ChatActivity.onMessageReceived -> 消息收到了")
            }
        }
    }

    override fun onCmdMessageReceived(p0: MutableList<EMMessage>?) {
    }

    override fun onMessageRead(p0: MutableList<EMMessage>?) {
    }

    override fun onMessageDelivered(p0: MutableList<EMMessage>?) {
    }

    override fun onMessageRecalled(p0: MutableList<EMMessage>?) {
    }

    override fun onMessageChanged(p0: EMMessage?, p1: Any?) {
    }

    override fun onResume() {
        super.onResume()
        EMClient.getInstance().chatManager().addMessageListener(this)
    }



}